package com.gr.experimental.model.impl;

import java.util.ArrayList;
import java.util.List;

import com.gr.experimental.model.RVClass;
import com.gr.experimental.model.RVState;
import com.gr.experimental.model.UID;

class RVClassImpl implements RVClass {

	private String name;
	
	private List<RVState> domain;

	private int argumentCount;
	
	RVClassImpl(String name, List<UID> domain, int argumentCount)  {
		this.name = name;
		this.domain = new ArrayList<RVState>();
		// setting the domain 
		for (UID uid: domain) {
			this.domain.add(new RVStateImpl(this, uid));
		}
		this.argumentCount = argumentCount;

	}
	
	/**
	 * {@inheritDoc}
	 * @see com.gr.exp.reasoner.RVClass#getName()
	 */
	public String getName() {
		return this.name;
	}

	/**
	 * {@inheritDoc}
	 * @see com.gr.exp.reasoner.RVClass#getArgumentCount()
	 */
	public int getArgumentCount() {
		return this.argumentCount;
	}

	/**
	 * {@inheritDoc}
	 * @see com.gr.exp.reasoner.RVClass#setArgumentCount(int)
	 */
	public void setArgumentCount(int argumentCount) {
		this.argumentCount = argumentCount;
	}
	
	
	/**
	 * {@inheritDoc}
	 * @see com.gr.exp.reasoner.RVClass#getDomain()
	 */
	public List<RVState> getDomain() {

		// TODO: optimize domain by caching
		return domain;
	}

	/**
	 * {@inheritDoc}
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + this.argumentCount;
		result = prime * result
				+ ((this.domain == null) ? 0 : this.domain.hashCode());
		result = prime * result
				+ ((this.name == null) ? 0 : this.name.hashCode());
		return result;
	}

	/**
	 * {@inheritDoc}
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		final RVClass other = (RVClass) obj;
		if (this.argumentCount != other.getArgumentCount())
			return false;
		if (this.domain == null) {
			if (other.getDomain() != null)
				return false;
		} else if (!this.domain.equals(other.getDomain()))
			return false;
		if (this.name == null) {
			if (other.getName() != null)
				return false;
		} else if (!this.name.equals(other.getName()))
			return false;
		return true;
	}

	
	
//	// as for now we assume only one home mfrag for a resident variable
//	public MFrag homeMFrag;
//	
//	// the mfrags in which the random variable is an input node
//	// we still have to consider how to treat the random variables
//	// being inner variables of some input variables
//	public List<MFrag> inputMFrags;
//	
//	// the mfrags in which the random variable is a context node
//	public List<MFrag> contextMFrags;
//	
	
	
}
